package com.mid;

/**
 * Created by Lxk on 2019/6/7.
 */
public class Solution59 {

    public int[][] generateMatrix(int n) {
        int count = 1,min = 0,max = n-1;
        int[][] matrix = new int[n][n];
        while(min<=max){
            for(int i = min;i < max; i++){
                matrix[min][i] = count++;
            }
            for(int i = min;i < max; i++){
                matrix[i][max] = count++;
            }
            for(int i = max;i > min; i--){
                matrix[max][i] = count++;
            }
            for(int i = max;i > min; i--){
                matrix[i][min] = count++;
            }
            if(min==max){
                matrix[min][max] = count++;
            }
            max--;
            min++;
        }
        return matrix;
    }

    public static void main(String[] args) {
        Solution59 solution59 = new Solution59();
        solution59.generateMatrix(3);
    }

}
